//
//  YCDetailHtmlViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/7/3.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCDetailHtmlViewController.h"
#import "JX_WebView.h"
#import "YCShareCore.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AddPatViewController.h"

@interface YCDetailHtmlViewController ()<WKUIDelegate,UIWebViewDelegate>

//WebView
@property (nonatomic,strong) JX_WebView *webView;
//分享视图
@property(nonatomic, strong) YCShareCore *shareView;
//分享按钮
@property(nonatomic, weak) UIButton *shareBtn;
//分享链接
@property(nonatomic, copy) NSString *shareLink;
//为获取分享链接的title的webView
@property(nonatomic, strong) WKWebView *titleWebView;
//分享链接的title
@property(nonatomic, copy) NSString *shareLinkTitle;

@end

@implementation YCDetailHtmlViewController

- (void)setModel:(YCSellRightModel *)model
{
    _model = model;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //进行配置控制器
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    //实例化对象
    configuration.userContentController = [WKUserContentController new];
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    configuration.preferences = preferences;
    
    self.webView =[[JX_WebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64-49)configuration:configuration];
    [_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [self.view addSubview:_webView];
    _webView.backgroundColor = [UIColor clearColor];
    
    NSString *urlString = [self.model.SKIP_TO stringByAppendingString:@"&footer=hide"];
    _webView.UIDelegate = self;
    
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    
    [_webView webViewLoadStart:^(WKWebView *webview,WKNavigation *navigation) {
        [self presentLoadingTips:nil];
    } Finish:^(WKWebView *webview,WKNavigation *navigation) {
        [self dismissTips];
        self.shareBtn.hidden = NO;
    } Fail:^(WKWebView *webview,WKNavigation *navigation) {
        [self dismissTips];
        self.shareBtn.hidden = YES;
    }];
    
    
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 60, kScreenHeight - 180, 54, 54)];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"矢量智能对象xx"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    
    
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 20, 10, 20);
    [backButton setBackgroundImage:[UIImage imageNamed:@"形状-2-拷贝-6"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    
    // 售卡分享按钮
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(0, 0, 40, 25);
    
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
    self.shareBtn = shareBtn;
    self.shareBtn.hidden = YES;
    
    self.shareLink = [NSString stringWithFormat:@"%@&user_id=%@&user_name=%@&link_mobile=%@",self.model.SKIP_TO,[YCUserTool sharedTools].userID,[[YCUserTool sharedTools].userName hdf_base64Encoding],[YCUserTool sharedTools].linkMobile];
    self.titleWebView = [[WKWebView alloc] init];
    [_titleWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    [_titleWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.shareLink]]];
    
    
    
    UIView *bottonView = [[UIView alloc]initWithFrame:CGRectMake(0,   kScreenHeight - 64-49, kScreenWidth, 49)];
    [self.view addSubview:bottonView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 49)];
    label.backgroundColor = [UIColor colorWithRGBValue:0xe7e7e7];
    label.textColor= [UIColor redColor];
    CGFloat money = [self.model.CARD_PRICE floatValue];
    label.text = [NSString stringWithFormat:@"¥ %.2f",money];
    label.textAlignment = NSTextAlignmentCenter;
    [bottonView addSubview:label];
    
    UIButton * buy= [UIButton buttonWithType:UIButtonTypeCustom];
    buy.frame = CGRectMake(120,  0, kScreenWidth-120, 49);
    [buy setTitle:@"立即购买" forState:UIControlStateNormal];
    buy.backgroundColor = [UIColor colorWithRGBValue:0x0092F0];
    [buy addTarget:self action:@selector(BuyClick) forControlEvents:UIControlEventTouchUpInside];
    
    [bottonView addSubview:buy];
    
    
    //因为跟微信公用一个页面，每一种卡的sale_type都不一样，所以根据这个来判断拦截就可以了
    [_webView contains:@"sale_type"
                    Do:^{
                        [self pushJoinVC];
                    }];
}

-(void)pushJoinVC{
    [self presentMessageTips:@"请在心脑管家中打开此链接!"];
}

//MARK: - 售卡列表分享
- (void)share:(UIBarButtonItem *)item{
    _shareView = [YCShareCore configWithTitle:self.shareLinkTitle
                                          Des:self.model.SHOW_DESC
                                   ThumbImage:self.model.CARD_PIC
                                         Link:self.shareLink
                              Completehandler:^(id result, NSError *error) {
                                  
                              }];
    
    _shareView.sharePlatform = YCSharePlatformWechatSession | YCSharePlatformWechatTimeLine | YCSharePlatformWechatFavorite;
    
    [self.shareView show];
}

//用苹果自带的返回键按钮处理如下(自定义的返回按钮)
- (void)back:(UIBarButtonItem *)btn
{

    [self.view resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)btnClick
{
    [self.webView reload];
}



//添加就诊人
-(void)BuyClick{
    

    AddPatViewController * vc =  [[AddPatViewController alloc]initWithNibName:@"AddPatViewController" bundle:nil];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
    
}


#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"title"])
    {
        if (object == self.webView) {
            self.navigationItem.title = self.webView.title;
            
        }else if (object == self.titleWebView){
            self.shareLinkTitle = self.titleWebView.title;
            
        }else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    else {
        
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)dealloc{
    [_webView removeObserver:self forKeyPath:@"title"];
    [_titleWebView removeObserver:self forKeyPath:@"title"];
}


@end
